<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/img/heima.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="active?'63px':'200px'" ref="asideW">
        <!-- 侧边栏菜单区 -->
        <div class="sousuo" @click="sousuo"></div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="active"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单的模版区 -->
            <template slot="title">
              <!-- 图标 -->
              <i class="el-icon-location"></i>
              <!-- 文 本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+zilei.path"
              v-for="zilei in item.children"
              :key="zilei.id"
              @click="saveNavState('/'+zilei.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文 本 -->
                <span>{{zilei.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import { request } from "../network/request.js";
import { request1 } from "../network/request.js";
export default {
  data() {
    return {
      menuList: [],
      active: false,
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.getMenuList1();
    this.activePath = window.sessionStorage.getItem("activePath");
    //console.log(this.activePath);
  },
  methods: {
    // 退出
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    //请求列表数据
    async getMenuList() {
      const res = await request({
        url: "/zl/menu",
        method: "get"
      });
      //console.log(res);
      if (res.data.code !== 0) return this.$message.error(res.data.msg);
      this.menuList = res.data.data.menu_list;
      // console.log(res);
    },
    async getMenuList1() {
      const res = await request1({
        url: "user",
        method: "post",
        data: this.loginForm
      });
      //console.log(res);
    },
    sousuo() {
      this.active = !this.active;
    },
    // 点击菜单高亮 将路径先存到sessionStorage里面，然后再到created里面获取，并将值赋给data中的
    // activePath
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);

      this.activePath = activePath;
    }
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  overflow: hidden;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #eaedf1;
}
.sousuo {
  height: 30px;
  width: 100%;
  background-color: #fff;
}
</style>